package com.gitee.xmhzzz.component.spring.cloud.config;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.List;

public class MvcRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor
{
    
    public MvcRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters)
    {
        super(converters);
    }
    
    public MvcRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters,
                                                 List<Object> requestResponseBodyAdvice)
    {
        super(converters, requestResponseBodyAdvice);
    }
    
    public MvcRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters,
                                                 ContentNegotiationManager manager, List<Object> requestResponseBodyAdvice)
    {
        super(converters, manager, requestResponseBodyAdvice);
    }
    
    @Override
    public boolean supportsParameter(MethodParameter parameter)
    {
        //springcloud的接口入参没有写@RequestBody，并且是自定义类型对象 也按JSON解析
        if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), FeignClient.class) && isCustomizedType(parameter.getParameterType())) {
            return true;
        }
        return super.supportsParameter(parameter);
    }
    
    private boolean isCustomizedType(Class<?> cls)
    {
       return cls.getName().startsWith("com.yipule");
    }
    
}
